layui.use(['element','laypage', 'layer'], function(){
    var element = layui.element;
    var laypage = layui.laypage
        ,layer = layui.layer;


});


$(function () {

    /**
     * 导航栏菜单高亮显示
     */

    var typeId = $("#type").val();
    $("#menu"+typeId+"").css("color","#45B6F7");
    $("#menu"+typeId+"").addClass("layui-this");

    initLayPage();


    showCategory();

})

/**
 * 查看文章详情页面
 * @param id
 */
function  desc(typeid,id) {
    location.href = "/article/"+id;
}

function showCategory(){
    var search = location.search;
    var searchLabelId = getSearchLabelId(search);

    $(".category").each(function (i,e) {
        if(searchLabelId == '') {
            if(i == 0) {
                $(e).addClass("current")
            }else {
                $(e).removeClass("current");
            }
        }else {
            var labelId = "cate-"+searchLabelId;
            var typeLabelId = $(e).attr("id");
            if(labelId == typeLabelId) {
                $(e).addClass("current")
            }else {
                $(e).removeClass("current");
            }
        }
    })
}

function getSearchLabelId(search){
    var labelId = '';
    if(search == '' || search == null) {
        return labelId;
    }else {
        var split = search.replace("?","&").split("&");
        split.forEach(function (e) {
            if(e.startsWith("labelId=")) {
                labelId = e.replace("labelId=", "")
                return labelId;
            }
        });
        return labelId;
    }
}

function initLayPage(pageConf) {
    if(!pageConf){
        pageConf = {};
        pageConf.limit = 30;
        pageConf.page = 1;
    }

    var typeId = $("#rel_typeId").val();

    layui.use(['element','laypage', 'layer'], function(){
                var element = layui.element;
                var laypage = layui.laypage
                    ,layer = layui.layer;

                var pageData = $("#pageData").val();
                var page = JSON.parse(pageData);

                laypage.render({
                    elem: 'page'
                    ,count: page.count
                    ,curr:page.page
                    ,limit:page.limit
                    ,theme: '#1E9FFF'
                    ,jump:function (obj,first) {
                        //首次不执行
                        if(!first){
                            var search = location.search;
                            var searchLabelId = getSearchLabelId(search);
                            if(searchLabelId != '') {
                                location.href = "/yangmao?page="+obj.curr+"&limit="+obj.limit+"&labelId="+searchLabelId;
                            }else {
                                location.href = "/yangmao?page="+obj.curr+"&limit="+obj.limit;
                            }

                        }
                    }
                });
            });
}


